`timescale 1ns/1ps

module tb_ecc_core();
reg clk;
reg rst_n;
reg Start;
wire Done;
reg [1:0] FunSel;
reg curve_sel;
reg [255:0] dA;
reg [255:0] PA_x;
reg [255:0] PA_y;
reg [255:0] random_k;
reg [255:0] s_verify;
reg [255:0] r_verify;
wire [255:0] s;
wire [255:0] r;
reg Hv_done;
reg [255:0] Hv_r;
wire bingo;
wire [2:0] State;
wire [7:0] error;
ecc_core uut (
    .clk(clk),
    .rst_n(rst_n),
    .Start(Start),
    .Done(Done),
    .FunSel(FunSel),
    .curve_sel(curve_sel),
    .dA(dA),
    .PA_x(PA_x),
    .PA_y(PA_y),
    .random_k(random_k),
    .s_verify(s_verify),
    .r_verify(r_verify),
    .s(s),
    .r(r),
    .Hv_done(Hv_done),
    .Hv_r(Hv_r),
    .bingo(bingo),
    .State(State),
    .error(error)
);
always #5 clk = ~clk;
task init_test_vectors;
begin
    curve_sel = 1'b1;
    dA = 256'h128B2FA7_BD433C6C_068C8D80_3DFF7979_2A519A55_171B1B65_0C23661D_15897263;
    PA_x = 256'h0AE4C779_8AA0F119_471BEE11_825BE462_02BB79E2_A5844495_E97C04FF_4DF2548A;
    PA_y = 256'h7C0240F8_8F1CD4E1_6352A73C_17B7F16F_07353E53_A176D684_A9FE0C6B_B798E857;
    Hv_r = 256'hF4A38489_E32B45B6_F876E3AC_2168CA39_2362DC8F_23459C1D_1146FC3D_BFB7BC9A;
    random_k = 256'h6CB28D99_385C175C_94F94E93_4817663F_C176D925_DD72B727_260DBAAE_1FB2F96F;
    r_verify = 256'h40F1EC59_F793D9F4_9E09DCEF_49130D41_94F79FB1_EED2CAA5_5BACDB49_C4E755D1;
    s_verify = 256'h6FC6DAC3_2C5D5CF1_0C77DFB2_0F7C2EB6_67A45787_2FB09EC5_6327A67E_C7DEEBE7;
end
endtask
task test_signature;
begin
    $display("--- Starting Signature Test ---");
    FunSel = 2'b00;
    Hv_done = 1'b1;
    Start = 1'b1;
    @(posedge clk);
    Start = 1'b0;
    
    wait(Done == 1'b1);
    $display("Signature completed at time %0t", $time);
    if (r === r_verify && s === s_verify) begin
        $display("PASS: Signature matches expected result");
        $display("r = %064h", r);
        $display("s = %064h", s);
    end
    else begin
        $display("FAIL: Signature does not match expected result");
        $display("Expected r = %064h", r_verify);
        $display("Actual   r = %064h", r);
        $display("Expected s = %064h", s_verify);
        $display("Actual   s = %064h", s);
    end
    
    #100;
    $display("\n--- Preparing for Verification Test ---");
    FunSel = 2'b01;
    Hv_done = 1'b1;
    Start = 1'b1;
    
    @(posedge clk);
    Start = 1'b0;
    
    wait(Done == 1'b1);
    $display("Verification completed at time %0t", $time);
    if (bingo) begin
        $display("PASS: Signature verification successful (bingo=1)");
    end
    else begin
        $display("FAIL: Signature verification failed (bingo=0)");
    end
    
    $display("Verification state: %b", State);
    $display("Error code: %h", error);
end
endtask
initial begin
    clk = 0;
    rst_n = 0;
    Start = 0;
    FunSel = 0;
    Hv_done = 0;
    Hv_r = 0;
    #10 rst_n = 1;
    init_test_vectors();
    test_signature();
    #100;
    $display("--- ECC Core Test Completed ---");
    $finish;
end
always @(posedge clk) begin
    if (Start) begin
        case (FunSel)
            2'b00: $display("Starting signature operation");
            2'b01: $display("Starting verification operation");
            default: $display("Starting unknown operation");
        endcase
    end
    
    if (Done) begin
        $display("Operation completed at time %0t", $time);
    end
end

endmodule